//
//  ILGALLiveTool.m
//  IfengLiveRecord
//
//  Created by zhangtongle-Pro on 2017/9/8.
//  Copyright © 2017年 凤凰新媒体. All rights reserved.
//

#import "ILGALLiveTool.h"

// 目前使用的SDK版本：3.1.0
#if __has_include(<AlivcLivePusher/AlivcLivePusherHeader.h>)

#import <AlivcLivePusher/AlivcLivePusherHeader.h>

@interface ILGALLiveTool () <AlivcLivePusherInfoDelegate, AlivcLivePusherErrorDelegate, AlivcLivePusherNetworkDelegate>

@property (nonatomic, strong) AlivcLivePusher *livePusher;
@property (nonatomic, assign) UIInterfaceOrientation videoDirection;

@end

#endif

@implementation ILGALLiveTool

#if __has_include(<AlivcLivePusher/AlivcLivePusherHeader.h>)

- (AlivcLivePusher *)livePusher {
    if (!_livePusher) {
        AlivcLivePushConfig *config = [self configure];
        if (_videoDirection == UIInterfaceOrientationLandscapeLeft) {
            config.orientation = AlivcLivePushOrientationLandscapeLeft;
        } else if (_videoDirection == UIInterfaceOrientationLandscapeRight) {
            config.orientation = AlivcLivePushOrientationLandscapeRight;
        } else {
            config.orientation = AlivcLivePushOrientationPortrait;
        }
        _livePusher = [[AlivcLivePusher alloc] initWithConfig:config];
    }
    return _livePusher;
}
- (void)startPreview:(UIView *)view
{
    [self startPreview:view inDirection:UIInterfaceOrientationPortrait];
}
- (void)startPreview:(UIView *)view inDirection:(UIInterfaceOrientation)direction
{
    _videoDirection = direction;
    
    int ret = [self.livePusher startPreview:view];
    if (ret != 0) {
        NSLog(@"开启预览失败%d", ret);
    }
}
- (void)stopPreview
{
    [self.livePusher stopPreview];
}

- (void)startPush:(NSString *)urlString
{
    if ([self.delegate respondsToSelector:@selector(ILGStreamStateChanged:)]) {
        [self.delegate ILGStreamStateChanged:ILGStreamStateConnecting];
    }
    
    int state = [self.livePusher startPushWithURL:urlString];
    if (state == 0) {
        if ([self.delegate respondsToSelector:@selector(ILGStreamStateChanged:)]) {
            [self.delegate ILGStreamStateChanged:ILGStreamStateConnectSuccess];
        }
    } else {
        // 推流失败
    }
}
- (void)pausePush
{
    if ([self.delegate respondsToSelector:@selector(ILGStreamStateChanged:)]) {
        [self.delegate ILGStreamStateChanged:ILGStreamStateDisconnecting];
    }
    
    [self.livePusher pause];
    
    if ([self.delegate respondsToSelector:@selector(ILGStreamStateChanged:)]) {
        [self.delegate ILGStreamStateChanged:ILGStreamStateIdle];
    }
}
- (void)stopPush
{
    [self.livePusher stopPush];
    
    [self.livePusher destory];
    self.livePusher = nil;
}

- (void)switchCamera
{
    [super switchCamera];
    [self.livePusher switchCamera];
}

- (void)adjustBeautyValue:(CGFloat)value
{
    [super adjustBeautyValue:value];

    int beautyLevel = value*10;
    self.livePusher.beautyBuffing = beautyLevel;
}

- (double)videoUploadBitrate
{
    return self.livePusher.getLivePushStatusInfo.videoUploadBitrate;
}

#pragma mark - 初始化

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self addObserver];
    }
    return self;
}

- (void)dealloc {
    
}

- (AlivcLivePushConfig *)configure {
//    NSString *sdkVersion = [self.livePusher getSDKVersion];
    
    AlivcLivePushConfig *config = [[AlivcLivePushConfig alloc] initWithResolution:AlivcLivePushResolution540P]; // 默认分辨率540P
    config.fps = AlivcLivePushFPS20; // 默认帧率20
    
    config.targetVideoBitrate = 1200; // 目标码率1200Kbps
    config.minVideoBitrate = 400; // 最小码率400Kbps
    config.initialVideoBitrate = 900; // 初始码率900Kbps
    
    config.videoEncodeGop = AlivcLivePushVideoEncodeGOP_2; // 默认关键帧间隔值为2
    
    config.previewMirror = false; // 关闭预览镜像
    
    config.orientation = AlivcLivePushOrientationLandscapeRight; // Left横屏推流
    config.cameraType = self.cameraPosition == AVCaptureDevicePositionBack ? AlivcLivePushCameraTypeBack:AlivcLivePushCameraTypeFront;
    
    // 美颜
    config.beautyOn = true; // 关闭开关
    // 美白范围0-100，建议设置范围0-30
    config.beautyWhite = 20;
    // 磨皮范围0-100，建议设置范围0-80
    config.beautyBuffing = 80;
    // 亮度范围0-100，建议设置范围0-50
    config.beautyBrightness = 10;
    // 红润设置范围0-100，建议设置范围0-50
    config.beautyRuddy = 10;
    // 饱和度设置范围0-100，建议设置范围40-60
    config.beautySaturation = 50;
    
    // 添加水印
    NSString *watermarkBundlePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"watermark"] ofType:@"png"];
    if (watermarkBundlePath.length) {
        [config addWatermarkWithPath: watermarkBundlePath
                     watermarkCoordX:0.1
                     watermarkCoordY:0.1
                      watermarkWidth:0.3];
    }
    
    return config;
}

- (void)addObserver {
    [self.livePusher setInfoDelegate:self];
    [self.livePusher setErrorDelegate:self];
    [self.livePusher setNetworkDelegate:self];
}

#pragma mark - Delegate
#pragma mark AlivcLivePusherErrorDelegate
- (void)onSystemError:(AlivcLivePusher *)pusher error:(AlivcLivePushError *)error
{
    
}

- (void)onSDKError:(AlivcLivePusher *)pusher error:(AlivcLivePushError *)error
{
    
}

#pragma mark AlivcLivePusherNetworkDelegate
- (void)onNetworkPoor:(AlivcLivePusher *)pusher {
    
}


/**
 推流链接失败
 
 @param pusher 推流AlivcLivePusher
 @param error error
 */
- (void)onConnectFail:(AlivcLivePusher *)pusher error:(AlivcLivePushError *)error {
    
}


/**
 网络恢复
 
 @param pusher 推流AlivcLivePusher
 */
- (void)onConnectRecovery:(AlivcLivePusher *)pusher {
    
}


/**
 重连开始回调
 
 @param pusher 推流AlivcLivePusher
 */
- (void)onReconnectStart:(AlivcLivePusher *)pusher {
    
}


/**
 重连成功回调
 
 @param pusher 推流AlivcLivePusher
 */
- (void)onReconnectSuccess:(AlivcLivePusher *)pusher {
    
}


/**
 重连失败回调
 
 @param pusher 推流AlivcLivePusher
 @param error error
 */
- (void)onReconnectError:(AlivcLivePusher *)pusher error:(AlivcLivePushError *)error {
    
}


/**
 发送数据超时
 
 @param pusher 推流AlivcLivePusher
 */
- (void)onSendDataTimeout:(AlivcLivePusher *)pusher {
    
}
#endif

@end
